package com.ehi.tsconfig.resource;

import com.ehi.common.jersey.status.ResponseBuilder;
import com.ehi.tsconfig.remote.AllianceService;
import com.ehi.tsconfig.service.ExecuteResult;
import com.ehi.tsconfig.service.TsconfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;

@Component
@Path("/")
public class TsconfigResource {
    @Autowired
    private TsconfigService tsService;

    @Value("${local.gitpath.fast-pi}")
    private String localGitPath;

    @Autowired
    private AllianceService service;

    @GET
    @Path("echo")
    public String echo() {
        String value = service.echo();
        return "Hello Spring Boot! " + value;
    }


    @Path("configfile")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response createTsConfig(@QueryParam("configJson") String configJson) {
        File outFile = new File(this.localGitPath + "/instance.setting.json");
        this.tsService.createConfigFile(outFile, configJson);
        return ResponseBuilder.ok(new Object());
    }


    @Path("clone")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response cloneProject() {
        ExecuteResult result = this.tsService.cloneProject();
        return ResponseBuilder.ok(result);
    }

    @Path("instance")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response newInstance() {
        return ResponseBuilder.ok(this.tsService.newInstance());
    }
}
